﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SPPropertyBagManagement.Code
{
    static class Utils
    {
        /// <summary>
        /// Finds the control.
        /// </summary>
        /// <param name="controlCollection">The control collection.</param>
        /// <param name="controlId">The control id.</param>
        /// <returns></returns>
        public static Control FindControl(ControlCollection controlCollection, string controlId)
        {
            foreach (Control ctrl in controlCollection)
            {
                if (ctrl.ID == controlId)
                {
                    return ctrl;
                }

                var control = FindControl(ctrl.Controls, controlId);

                if (control != null)
                {
                    return control;
                }
            }

            return null;
        }

        /// <summary>
        /// Sets the page title.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="title">The title.</param>
        public static void SetPageTitle(Page page, string title)
        {
            var master = page.Master;

            if (master == null)
            {
                return;
            }

            ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)master.FindControl("PlaceHolderPageTitle");

            if (contentPlaceHolder == null)
            {
                return;
            }

            contentPlaceHolder.Controls.Clear();

            LiteralControl literalControl = new LiteralControl();
            literalControl.Text = title;
            contentPlaceHolder.Controls.Add(literalControl);
        }

        /// <summary>
        /// Sets the page sub title.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="title">The title.</param>
        public static void SetPageSubTitle(Page page, string title)
        {
            var master = page.Master;

            if (master == null)
            {
                return;
            }

            ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder)master.FindControl("PlaceHolderPageTitleInTitleArea");

            if (contentPlaceHolder == null)
            {
                return;
            }

            contentPlaceHolder.Controls.Clear();

            LiteralControl literalControl = new LiteralControl();
            literalControl.Text = title;
            contentPlaceHolder.Controls.Add(literalControl);
        }

        /// <summary>
        /// Downloads the file to client.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="data">The data.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <exception cref="System.ArgumentNullException">page</exception>
        public static void DownloadFileToClient(Page page, string data, string fileName)
        {
            if (page == null)
            {
                throw new ArgumentNullException("page");
            }

            page.Response.Clear();
            page.Response.ClearContent();
            page.Response.ClearHeaders();
            page.Response.ContentType = "application/unknown";
            page.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

            page.Response.Write(data);

            page.Response.Flush();

            page.Response.End();
        }
    }
}
